package com.ykt.controller;

import com.jfinal.aop.Before;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;
import com.ykt.model.UserAddress;
import com.ykt.service.UserAddressService;
import com.ykt.validator.CommonValidator;
import com.ykt.validator.UserAddressValidator;
import com.ykt.vo.ResultStatus;
import com.ykt.vo.ResultVo;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户地址操作 
 * @author wing
 * @version 1.0.0
 */
public class UserAddressController extends BaseController{

	private UserAddressService userAddressService = enhance(UserAddressService.class.getName(), UserAddressService.class);

    @Before(CommonValidator.UserIdValidator.class)
    public void find(){
    	Integer userId = getParaToInt("userId", -1);

    	PageParam<Integer> param = super.toPageParam(userId);
        PageData<UserAddress> pageData = userAddressService.pageFindByUserId(param);

    	this.returnJson(ResultVo.success(pageData));
    }

    @Before(UserAddressValidator.CreateValidator.class)
    public void create(){
    	Integer userId = getParaToInt("userId", -1);
    	String provinceId = getPara("provinceId");
    	String cityId = getPara("cityId");
    	String districtId = getPara("districtId");
    	String address = getPara("address");
    	String receiptName = getPara("receiptName");
    	String receiptPhone = getPara("receiptPhone");

    	UserAddress model = new UserAddress();
    	model.set("user_id", userId);
    	model.set("province_id", provinceId);
    	model.set("city_id", cityId);
    	model.set("district_id", districtId);
    	model.set("address", address);
    	model.set("receipt_name", receiptName);
    	model.set("receipt_phone", receiptPhone);
    	
    	int id = userAddressService.save(model);

    	Map<String, Object> resultMap = new HashMap<>();
    	if (id > 0) {
    		resultMap.put("id", id);
            returnJson(ResultVo.success(resultMap));
        } else {
            returnJson(ResultVo.failure(null, ResultStatus.ADD_FAILURE_MSG));
        }
    }

    @Before({UserAddressValidator.AddressIdValidator.class, UserAddressValidator.CreateValidator.class})
    public void modify(){
    	Integer addressId =  getParaToInt("addressId", -1);
    	String provinceId = getPara("provinceId");
    	String cityId = getPara("cityId");
    	String districtId = getPara("districtId");
    	String address = getPara("address");
    	String receiptName = getPara("receiptName");
    	String receiptPhone = getPara("receiptPhone");

    	UserAddress entity = userAddressService.find(addressId);
    	entity.set("province_id", provinceId);
    	entity.set("city_id", cityId);
    	entity.set("district_id", districtId);
    	entity.set("address", address);
    	entity.set("receipt_name", receiptName);
    	entity.set("receipt_phone", receiptPhone);
    	
    	int id = userAddressService.save(entity);
    	if (id > 0) {
            returnJson(ResultVo.success());
        } else {
            returnJson(ResultVo.failure(null, ResultStatus.UPDATE_FAILURE_MSG));
        }
    }

    @Before(UserAddressValidator.AddressIdValidator.class)
    public void del(){
    	Integer addressId =  getParaToInt("addressId", -1);
    	userAddressService.delete(addressId);
    	
    	this.returnJson(ResultVo.success());
    }
    
    //设置默认地址
    @Before({CommonValidator.UserIdValidator.class, UserAddressValidator.AddressIdValidator.class})
    public void setDefaults(){
    	Integer userId = getParaToInt("userId", -1);
    	Integer addressId =  getParaToInt("addressId", -1);

    	userAddressService.setDefaults(addressId, userId);
    	
    	this.returnJson(ResultVo.success());
    }

    @Before(CommonValidator.UserIdValidator.class)
	public void findDefaults(){
		Integer userId = getParaToInt("userId", -1);
		UserAddress entity = userAddressService.findDefaults(userId);

		this.returnJson(ResultVo.success(entity));
	}
}
